package com.example.springboot.controller;

import com.example.springboot.common.Result;
import com.example.springboot.controller.request.RoomPageRquest;
import com.example.springboot.entity.Room;
import com.example.springboot.service.IRoomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/room")
public class RoomController {

    @Autowired
    IRoomService roomService;

    @GetMapping("/list")
    public List<Room> listRooms() {
        return roomService.listRooms();
    }

    @PostMapping("/add")
    public Result add(@RequestBody Room room) {
        roomService.add(room);
        return Result.success();
    }

    @PostMapping("/delete")
    public Result delete(@RequestBody Room room) {
        roomService.delete(room);
        return Result.success();
    }

    @GetMapping("/page")
    public Result page(RoomPageRquest roomPageRquest) {
        return Result.success(roomService.page(roomPageRquest));

    }

    @GetMapping("/{roomid}")
    public Result getById(@PathVariable String roomid) {
        Room room = roomService.getById(roomid);
        return (Result) Result.success(room);
    }

    @PutMapping("/update")
    public Result update(@RequestBody Room room) {
        roomService.update(room);
        return Result.success();
    }

}
